﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using Coolite.Ext.Web;
using GUESTSMANAGEMENT.Controller;

namespace GUESTSMANAGEMENT.View.Report
{
    public partial class gridReport : System.Web.UI.UserControl
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            
        }
        protected void EnventDateSelected(object sender, EventArgs e)
        {
            //DateTime data = DateTime.Now;
            //Ext.Msg.Alert("Hora", "A hora é:" + data);
            using (guestsmanagementEntities context = new guestsmanagementEntities())
            {
                try
                {
                    DateTime date = this.eventDate.SelectedDate;
                    gmevent evento = Controller.Event.getByEventDate(date);

                    if (evento != null)
                    {
                       // int cdUser = Convert.ToInt32(Session["userCd"]);

                        var selectListGuests = from guestlists in context.gmlistguests.Include("gmlistguests.cdlistguests")
                                               from gevent in context.gmevent.Include("gmevent.nmevent")
                                               from user in context.gmuser.Include("gmuser.nmuser")
                                               //from guests in context.gmguest.Include("gmguests.nmguests")
                                               where guestlists.GMUSER_CDUSER == user.CDUSER && gevent.CDEVENT == guestlists.GMEVENT_CDEVENT 
                                               //guests.CDGUEST == guestlists.GMGUEST_CDGUEST
                                               && gevent.DTEVENT == date
                                               group guestlists by new
                                               {
                                                   guestlists.GMUSER_CDUSER,
                                                   user.NMUSER,
                                                   gevent.NMEVENT,
                                                   gevent.DTEVENT
                                               }
                                                   into g
                                                   select new
                                                   {
                                                       g.Key.NMUSER,
                                                       g.Key.NMEVENT,
                                                       g.Key.DTEVENT,
                                                       TOTAL = g.Count(),
                                                       CONFIRMADOS = g.Count(a => a.TMCONFIRM != null)
                                                   };

                        var lista = selectListGuests.ToList();
                        //List<gmlistguests> listGuests = Controller.GuestsList.getGridListGuests(cdUser, (int)evento.CDEVENT);

                        this.reportStore.DataSource = lista;
                        this.reportStore.DataBind();

                        this.eventName.Text = evento.NMEVENT;
                        this.reportGridPanel.Enabled = true;
                        //this.eventName.Text = evento.CDEVENT.ToString();
                        //this.txt_idCdUser.Text = cdUser.ToString();
                        //GMListsGuests.SetEvent((int)evento.CDEVENT);
                    }
                    else
                    {
                        Ext.Msg.Alert("Alerta!", "Não há eventos cadastros nesta data!").Show();
                        this.reportGridPanel.Reload();
                    }
                }
                catch
                {
                    Ext.Msg.Alert("Alerta!", "Ocorreu um erro durante a transação! Tente novamente!").Show();
                }
            }
        }
        protected void filterDate(object sender, AjaxEventArgs e)
        { 
        }
    }
}